package com.ttc.mBackup.BackupSMS;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.os.Environment;
import android.util.Log;

public class FileBrowser{
	private String filename;
	private int version;
	private long timeCreate;
	private int smsCount;
	
	public FileBrowser(){
		super();
	}
	
	public FileBrowser(String filename){
		super();
		this.filename = filename;
	}
	
	
	
   public String getFilename() {
		return filename;
	}

	public int getVersion() {
		return version;
	}

	public long getTimeCreate() {
		return timeCreate;
	}

	public int getSmsCount() {
		return smsCount;
	}


public List<String> getDir()
    {	
    	List<String> path = new ArrayList<String>();
    	String filePath = Environment.getExternalStorageDirectory() + "/BACKUP/";
     
	     File f = new File(filePath);
	     String[] filenames = f.list();
	     for(String filename : filenames){
	    	 if(filename != null)
	    		 path.add(filename);	 
	     }
	     return path;
    }
    
    public boolean DeleleFile(){
    	File filepath = new File(Environment.getExternalStorageDirectory() + "/BACKUP/", filename);
    	
    	boolean result = false;
    	if (filepath.exists()) {
			result = filepath.delete();
		}
    	return result;
    }
    
    /**
	 * Readfile a file and return the content of it
	 * @param filename
	 * @return
	 * @throws IOException
	 */
	public String ReadBackupFile() throws IOException{
		File file = new File(Environment.getExternalStorageDirectory() + "/BACKUP/", filename);
		FileReader fileIn = null;
		try {
			fileIn = new FileReader(file);
		} catch (FileNotFoundException e) {
			Log.e("File Reading Exception", "Error in reading file " + e.getMessage());
			return "";
		}
		BufferedReader buffer = new BufferedReader(fileIn);
		String jsonContent = "";
		String s = "";
		while ((s = buffer.readLine()) != null) {
			jsonContent += s;
		}
		Log.v("check", "Read file Successfully");
		buffer.close();
		fileIn.close();
		return jsonContent;		
	}
	public boolean makeBackupFile(String filename, String json){
		File fileDir = new File(Environment.getExternalStorageDirectory()+ "/BACKUP");
		fileDir.mkdir();
		
		File file = new File(Environment.getExternalStorageDirectory() + "/BACKUP/", filename);
		file.delete();
		try {
			file.createNewFile();
		} catch (IOException e) {
			Log.e("IOException", "Can not create file " + e.getMessage());
			return false;
		}
		FileWriter fileOut = null;
		try {
			fileOut = new FileWriter(file);
		} catch (IOException e) {
			Log.e("IOException", "Can not read file " + e.getMessage());
			return false;
		}
		
		try {
			fileOut.write(json);
			fileOut.flush();
			fileOut.close();
			Log.v("check", "Create File Successfully");
		} catch (IOException e) {
			Log.e("IOException", "Can not read file " + e.getMessage());
			return false;
		}
		return true;
	}
    
}